	
	package Dao.daoimpl;
	
	import java.sql.Connection;
	import java.sql.ResultSet;
	import java.sql.SQLException;
	import java.sql.Statement;
	import java.util.ArrayList;
	import java.util.List;
	import Dao.TitleDao;
	import unitls.DataBaseConnection;
	import enitys.Title;
	
	public class TitleDaoImpl implements TitleDao{
		private Connection conn = null;
		private Statement stmt = null;
		private ResultSet rs = null;
		private String sql = null;// sql语句
	
		public List<Title> findTitleById(int id, boolean isAll) {
	
			// 根据isAll选择sql语句
			if (isAll) {
				//查询全部
				this.sql = "select * from title";
			} else {
				//按id查询
				this.sql = "select * from title where id = '" + id + "'";
			}
			List<Title> info = new ArrayList<Title>();
			// 连接数据库
			this.conn = DataBaseConnection.getConnection();
			try {
				this.stmt = this.conn.createStatement();
				this.rs = this.stmt.executeQuery(this.sql);
				//封装结果Bean
				while (this.rs.next()) {
					Title temp = new Title();
					temp.setId(this.rs.getInt("id"));
					temp.setHead(this.rs.getString("head"));
					temp.setText(this.rs.getString("text"));
					temp.setTime(this.rs.getString("time"));
					info.add(temp);
				}
				return info;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeRs(this.rs);
				DataBaseConnection.closeStmt(this.stmt);
				DataBaseConnection.closeConn(this.conn);
			}
			//若失败返回null
			return null;
		}
	
		public boolean addTitle(Title title) {
	
			//设置sql语句
			this.sql = "insert into title(head,text,time) values('" + title.getHead() + "','" + title.getText() + "','"
					+ title.getTime() + "')";
			this.conn = DataBaseConnection.getConnection();
			try {
				this.stmt = this.conn.createStatement();
				this.stmt.executeUpdate(this.sql);
				return true;
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeStmt(this.stmt);
				DataBaseConnection.closeConn(this.conn);
			}
			return false;
		}
	
		public int delTitleById(int id) {
	
			//设置sql语句
			this.sql = "delete from title where id='" + id + "'";
			this.conn = DataBaseConnection.getConnection();
			try {
				this.stmt = this.conn.createStatement();
				int rs = this.stmt.executeUpdate(this.sql);
				return rs;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeStmt(this.stmt);
				DataBaseConnection.closeConn(this.conn);
			}
			//失败返回删除0条
			return 0;
		}
	
		public boolean modifyTitleById(Title title, int id) {
	
			//设置sql语句
			this.sql = "update title set head='" + title.getHead() + "',text='" + title.getText() + "' where id = '" + id
					+ "'";
			this.conn = DataBaseConnection.getConnection();
			try {
				this.stmt = this.conn.createStatement();
				this.stmt.executeUpdate(this.sql);
				return true;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeStmt(this.stmt);
				DataBaseConnection.closeConn(this.conn);
			}
			return false;
		}
	}
